package com.shanghaiyimu.yimiyangche.activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.HuanluntaiAdapter;
import com.shanghaiyimu.yimiyangche.bean.LunTaiBeen;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.shanghaiyimu.yimiyangche.R.id.xiaoliangpaixu;


public class HuanLunTaiActivity extends AppCompatActivity implements View.OnClickListener{

    private ListView listView;
    private TextView paixu,shaixuan;
    private CheckBox zhidingyuanpeiluntai;
    private List<LunTaiBeen> datas;
    private List<LunTaiBeen> datas2 = new ArrayList<>();
    private  List<LunTaiBeen> datas3 = new ArrayList<>();
    private HuanluntaiAdapter adapter;
    private PopupWindow popupWindow;
    private View popView;
    private TextView tuijian,pingfen,xiaoliang,gaopricae,diprice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huan_lun_tai);
        initData();
        initView();
        initSetLinsener();
    }

    private void initSetLinsener() {
        zhidingyuanpeiluntai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setWrite();
                paixu.setTextColor(Color.BLACK);

                if (isChecked){
                    datas3.addAll(datas);
                    datas.clear();
                    datas.addAll(datas2);
                    zhidingyuanpeiluntai.setTextColor(Color.RED);
                }else {
                    datas.clear();
                    datas.addAll(datas3);
                    zhidingyuanpeiluntai.setTextColor(Color.BLACK);

                }
                adapter.notifyDataSetChanged();

            }

        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    private void initData() {
        datas = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            LunTaiBeen goods = new LunTaiBeen();
            goods.setName("玲珑轮胎 LMA16 175/70R14 LingLong"+i);
            goods.setPrice(52+i);
            goods.setXiaoliang(520-i);
            int j = i%5;
            if (j==0){
                j=5;
            }
            goods.setAssess(j);
            datas.add(goods);
        }
        datas2.addAll(datas);
//        adapter.notifyDataSetChanged();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.huanluntai_lv);
        adapter = new HuanluntaiAdapter(this,datas);
        listView.setAdapter(adapter);

        paixu = (TextView) findViewById(R.id.textView);
        shaixuan = (TextView) findViewById(R.id.shaixuan);
        zhidingyuanpeiluntai = (CheckBox) findViewById(R.id.zhidingyuanpeiluntai);
        paixu.setOnClickListener(this);
        shaixuan.setOnClickListener(this);

        popView = LayoutInflater.from(this).inflate(R.layout.popwindow_huanluntai,null);
        popView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        tuijian = (TextView) popView.findViewById(R.id.tuijainpaixu);
        pingfen = (TextView) popView.findViewById(R.id.pingfenpaixu);
        xiaoliang = (TextView) popView.findViewById(xiaoliangpaixu);
        diprice = (TextView) popView.findViewById(R.id.jiagedipaixu);
        gaopricae = (TextView) popView.findViewById(R.id.jiagegaopaixu);
        tuijian.setOnClickListener(this);
        pingfen.setOnClickListener(this);
        xiaoliang.setOnClickListener(this);
        diprice.setOnClickListener(this);
        gaopricae.setOnClickListener(this);


        popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        //防止无法关闭popupWindow
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        //点击popupWindow外部会不会自动关闭
        popupWindow.setOutsideTouchable(true);
        //如果要放置背景颜色#FFD700
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x99000000)); //设置背景显示图片


    }



    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.textView:

                popupWindow.showAsDropDown(paixu);
                break;
            case R.id.shaixuan:
                break;

            //pop
            case R.id.tuijainpaixu:
                zhidingyuanpeiluntai.setChecked(false);
                shaixuan.setTextColor(Color.BLACK);
                setWrite();
                paixu.setText(tuijian.getText());
                tuijian.setTextColor(Color.RED);
                datas.clear();
                datas.addAll(datas2);
                adapter.notifyDataSetChanged();
                popupWindow.dismiss();
                paixu.setTextColor(Color.RED);
                break;
            case R.id.pingfenpaixu:
                zhidingyuanpeiluntai.setChecked(false);
                shaixuan.setTextColor(Color.BLACK);
                paixu.setTextColor(Color.RED);
                setWrite();
                paixu.setText(pingfen.getText());
                pingfen.setTextColor(Color.RED);
                Collections.sort(datas, new Comparator<LunTaiBeen>() {
                    @Override
                    public int compare(LunTaiBeen o1, LunTaiBeen o2) {
                        if (o1.getAssess()>o2.getAssess()){
                            return -1;
                        }if (o1.getAssess() == o2.getAssess()){
                            return  0 ;
                        }else {
                            return 1;
                        }
                    }
                });
                adapter.notifyDataSetChanged();
                popupWindow.dismiss();
                break;
            case R.id.xiaoliangpaixu:
                zhidingyuanpeiluntai.setChecked(false);
                shaixuan.setTextColor(Color.BLACK);
                paixu.setTextColor(Color.RED);
                setWrite();
                paixu.setText(xiaoliang.getText());
                xiaoliang.setTextColor(Color.RED);
                Collections.sort(datas, new Comparator<LunTaiBeen>() {
                    @Override
                    public int compare(LunTaiBeen o1, LunTaiBeen o2) {
                        if (o1.getXiaoliang()>o2.getXiaoliang()){
                            return -1;
                        }if (o1.getXiaoliang() == o2.getXiaoliang()){
                            return  0 ;
                        }else {
                            return 1;
                        }
                    }
                });
                adapter.notifyDataSetChanged();
                popupWindow.dismiss();
                break;
            case R.id.jiagedipaixu:
                zhidingyuanpeiluntai.setChecked(false);
                shaixuan.setTextColor(Color.BLACK);
                paixu.setTextColor(Color.RED);
                setWrite();
                paixu.setText(diprice.getText());
                diprice.setTextColor(Color.RED);
                Collections.sort(datas, new Comparator<LunTaiBeen>() {
                    @Override
                    public int compare(LunTaiBeen o1, LunTaiBeen o2) {
                        if (o1.getPrice()>o2.getPrice()){
                            return 1;
                        }if (o1.getPrice() == o2.getPrice()){
                            return  0 ;
                        }else {
                            return -1;
                        }
                    }
                });
                adapter.notifyDataSetChanged();
                popupWindow.dismiss();
                break;
            case R.id.jiagegaopaixu:
                zhidingyuanpeiluntai.setChecked(false);
                shaixuan.setTextColor(Color.BLACK);
                paixu.setTextColor(Color.RED);
                setWrite();
                paixu.setText(gaopricae.getText());
                gaopricae.setTextColor(Color.RED);
                Collections.sort(datas, new Comparator<LunTaiBeen>() {
                    @Override
                    public int compare(LunTaiBeen o1, LunTaiBeen o2) {
                        if (o1.getPrice()>o2.getPrice()){
                            return -1;
                        }if (o1.getPrice() == o2.getPrice()){
                            return  0 ;
                        }else {
                            return 1;
                        }
                    }
                });
                adapter.notifyDataSetChanged();
                popupWindow.dismiss();
                break;

        }
    }
    private void setWrite(){
//        zhidingyuanpeiluntai.setTextColor(Color.BLACK);
//        shaixuan.setTextColor(Color.BLACK);
//        paixu.setTextColor(Color.BLACK);
        tuijian.setTextColor(Color.BLACK);
        pingfen.setTextColor(Color.BLACK);
        xiaoliang.setTextColor(Color.BLACK);
        diprice.setTextColor(Color.BLACK);
        gaopricae.setTextColor(Color.BLACK);

    }
}
